package com.shengdong.water.code.utils

import android.content.Context
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.IOException

object JsonUtil {
    private val gson = Gson()

    /**
     * Map 转 JSON RequestBody
     */
    fun mapToJsonRequestBody(map: Map<String, Any>): RequestBody {
        val jsonString = gson.toJson(map)
        return jsonString.toRequestBody("application/json".toMediaType())
    }

    /**
     * Map 转 JSON 字符串
     */
    fun mapToJson(map: Map<String, Any>): String {
        return gson.toJson(map)
    }



    inline fun <reified T> parseJsonToBean(jsonString: String): T? {
        return try {
            Gson().fromJson(jsonString, object : TypeToken<T>() {}.type)
        } catch (e: Exception) {
            null
        }
    }
}